package cn.iocoder.yudao.module.student.service.studentFeedback;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.student.controller.app.studentFeedback.vo.StudentFeedbackCreateReqVO;
import cn.iocoder.yudao.module.student.controller.app.studentFeedback.vo.StudentFeedbackPageReqVO;
import cn.iocoder.yudao.module.student.controller.app.studentFeedback.vo.StudentFeedbackUpdateReqVO;
import cn.iocoder.yudao.module.student.dal.dataobject.studentFeedback.StudentFeedbackDO;
import cn.iocoder.yudao.module.student.dal.mysql.studentFeedback.StudentFeedbackMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 实践项目反馈 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class StudentFeedbackServiceImpl implements StudentFeedbackService {

    @Resource
    private StudentFeedbackMapper studentFeedbackMapper;

    @Override
    public Long createPraxisProjectFeedback(StudentFeedbackCreateReqVO createReqVO) {
        // 插入
        StudentFeedbackDO feedbackDO =new StudentFeedbackDO();
        BeanUtils.copyProperties(createReqVO,feedbackDO);
        feedbackDO.setUpdateTime(new Date());
        String loginUserName = SecurityFrameworkUtils.getLoginUser().getUsername();
        feedbackDO.setUpdater(loginUserName);
        feedbackDO.setCreator(loginUserName);
        studentFeedbackMapper.insert(feedbackDO);
        // 返回
        return feedbackDO.getId();
    }

    @Override
    public void updatePraxisProjectFeedback(StudentFeedbackUpdateReqVO updateReqVO) {
        // 更新
        StudentFeedbackDO feedbackDO =new StudentFeedbackDO();
        BeanUtils.copyProperties(updateReqVO,feedbackDO);
        String loginUserName = WebFrameworkUtils.getLoginUserName();
        feedbackDO.setUpdater(loginUserName);
        feedbackDO.setCreator(loginUserName);
        feedbackDO.setUpdateTime(new Date());
        feedbackDO.setFeedbackTime(new Date());
        studentFeedbackMapper.updateById(feedbackDO);
    }

    @Override
    public void deletePraxisProjectFeedback(Long id) {
//        // 校验存在
//        this.validatePraxisProjectFeedbackExists(id);
//        // 删除
//        studentFeedbackMapper.deleteById(id);
    }

    private void validatePraxisProjectFeedbackExists(Long id) {
        if (studentFeedbackMapper.selectById(id) == null) {

        }
    }

    @Override
    public StudentFeedbackDO getPraxisProjectFeedback(Long id) {
        return studentFeedbackMapper.selectById(id);
    }

    @Override
    public List<StudentFeedbackDO> getPraxisProjectFeedbackList(Collection<Long> ids) {
        return studentFeedbackMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<StudentFeedbackDO> getPraxisProjectFeedbackPage(StudentFeedbackPageReqVO pageReqVO) {
        Long loginUserId = WebFrameworkUtils.getLoginUserId();
        pageReqVO.setStudentId(loginUserId);
        return studentFeedbackMapper.selectPage(pageReqVO);
    }

}
